//
//  HistoryViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/8/14.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

@objc protocol HistoryCtrlDelegate {
    func didSelect(controller:NSViewController,string data:String,row:Int)
}

class HistoryViewController: NSViewController {
    
    var dataList:[String] = []
    
    weak var delegate:HistoryCtrlDelegate?
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 200, 100))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        view.layer?.borderColor = color_gray(220).cgColor
        view.layer?.borderWidth = 0.6
        view.layer?.cornerRadius = 10
        self.view = view
    }
    
    var kSize:CGSize = CGSize.init(width: 400, height: 200) {
        didSet{
            self.view.setFrameSize(kSize)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        self.dataList = PLISTSaveManager.readList()
        if self.dataList.count > 0 {
            self.dataList.append("{\"name\":\"\(mt_history_clear)\",\"roomId\":\"0000\"}")
        }
        
        self.tabView.reloadData()
    }
    
    
    func setupUI() {
        self.view.addSubview(tabView)
        tabView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    
    lazy var tabView: ZHTableView = {
        let tab = ZHTableView.init(frame: NSRect.zero)
        tab.tabView.backgroundColor = color_gray(255)
        tab.tabView.gridColor = color_gray(255)
        tab.delegate = self
        tab.dataSourse = self
        return tab
    }()
}

extension  HistoryViewController:ZHTableViewDelegate,ZHTableViewDataSource {
    func zh_tableView(_ tableView: ZHTableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataList.count
    }
    
    func zh_tableView(_ tableView: ZHTableView, cellForRowAt indexPath: IndexPath) -> NSView? {
        var cell = tableView.tabView.makeView(withIdentifier: ZHTableViewCell.identifier, owner: self) as? HistoryCell
        if cell == nil {
            cell = HistoryCell.init(identifier: HistoryCell.identifier)
            cell?.setHover(color: color_gray(200))
        }
        let data = self.dataList[indexPath.item]
        let mode = Plist_historyMode.deserialize(from: data.toDict())
        cell?.stitle.stringValue = mode?.name ?? ""
        cell?.message.stringValue = mode?.roomId ?? ""
        cell?.isClear = mode?.name == mt_history_clear
        return cell
    }
    
    
    func zh_tableView(_ tableView: ZHTableView, didSelectRowAt indexPath: IndexPath) {
        Lk.shared.dismiss()
        if delegate != nil {
            self.delegate?.didSelect(controller: self, string: self.dataList[indexPath.item], row: indexPath.item)
        }
    }
    
    func zh_tableView(_ tableView: ZHTableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 26
    }
}
